"""
数据处理层
    专门进行数据处理
"""
import json
import os 
from conf import setting

def select(username):
      #（2）检验用户是否存在
    path =os.path.join(setting.DATA_PATH,f"{username}.json") #f"{user_name}.json" 格式化成json类型     
    if os.path.exists(path):
    #（3）若存在则打开数据返回给接口层
        with open(path,"r",encoding="utf-8") as f:
            user_dic = json.load(f)
            return user_dic
    #（4）不return默认返回none

def save(user_dic):
    #拼接用户的字典
    user_name = user_dic.get("user_name") 
    # print(user_name)
    path =os.path.join(setting.DATA_PATH,f"{user_name}.json") #f"{user_name}.json" 格式化成json类型
    #以用户名来设置存储文件               
    with open(path,"w",encoding="utf-8") as f:
        json.dump(user_dic,f,ensure_ascii=False)#ensure_ascii=False展示的时候可以是中文，就是好看
